﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CustomerService
{
    public partial class ContactPointList : System.Web.UI.UserControl
    {
        protected tblCustomer _customer;
        protected SalesManagerEntities _context;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                for (int i = 0; i < ListView1.Items.Count; i++)
                {
                    int key = (int)ListView1.DataKeys[i].Value;
                    string value = ((TextBox)ListView1.Items[i].FindControl("txtContactPointValue")).Text;
                    _context.tblContactPoint.Find(new object[] { key }).Value = value;
                }
            }
        }

        public new void Load(SalesManagerEntities context, tblCustomer customer)
        {
            _context = context;
            _customer = customer;
            if (!IsPostBack)
            {
                ListView1.DataSource = _customer.Person.ContactPoints;
                ListView1.DataBind();
            }
        }

        protected void btnRemove_Click(object sender, CommandEventArgs e)
        {
            int key = int.Parse(e.CommandArgument.ToString());
            _context.tblContactPoint.Remove(_context.tblContactPoint.Find(new object[] { key }));
            _context.SaveChanges();
            ListView1.DataSource = _context.tblContactPoint.Local.Where(t=>t.Person == _customer.Person);
            ListView1.DataBind();
        }
    }
}